// pages/home/pages/party1Proposal/party1Proposal.js
const app = getApp()
var r = require('../../../../utils/request.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    type: 0, //分类

    page: 1,
    pages: 0,

    list: [],
  },

  // 切换
  tabs(e) {
    let num = e.currentTarget.dataset.num
    this.setData({
      type: num,
      page: 1,
      pages: 0,
      list: []
    })
    this.datalist(1);
  },

  // 前往发布
  release() {
    wx.navigateTo({
      url: '../party1ProposalAdd/party1ProposalAdd',
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      page: 1,
      pages: 0,
      list: []
    })
    this.datalist(1);
  },

  //加载数据列表
  datalist(pageNo) {
    this.loading = true
    wx.showLoading()

    let url = ''
    if (this.data.type == 0) {
      url = '/jyxcAdvice/jyxcList'
    } else if (this.data.type == 1) {
      url = '/jyxcAdvice/jyxcListForUser'
    }
    var param = {
      userId: wx.getStorageSync('userId'),
      page: pageNo,
      limit: 10
    };
    //获取数据列表
    r.toGet(url, param, (res) => {
      this.loading = false
      wx.hideLoading()
      if (!res.retData || !res.retData.records || res.retData.records.length == 0) {
        // this.setData({
        //   show:true
        // })
        wx.showToast({
          title: '暂无数据',
          icon: 'none'
        })
        return
      } else {
        this.setData({
          // show:false,
          page: pageNo, //当前的页号
          pages: res.retData.pages, //总页数
          list: this.data.list.concat(res.retData.records)
        })
      }
    })
  },

  submit(e){
    let that = this
    let id = e.currentTarget.dataset.id
    let type = e.currentTarget.dataset.type
    if(type == 1){
      wx.showModal({
        title: '触发敏感词！！！',
        content: '您已触发敏感词，提交有禁言风险，是否确认？',
        success: function (res) {
          if (res.confirm) {
            that.tijiao(id)
          } else if (res.cancel) {
            console.log('用户点击取消')
          }
        }
      })
    }else{
      that.tijiao(id)
    }
    
  },

  tijiao(id){
    var that = this
    r.toPost('/jyxcAdvice/adviceUpdate', {id:id}, (res) => {
      wx.showToast({
        title: '提交成功',
        icon:'none'
      })
      setTimeout(function(){
        that.setData({
          page: 1,
          pages: 0,
          list: []
        })
        that.datalist(1);
      },1000)
    })
  },

  delet(e){
    let that = this
    let id = e.currentTarget.dataset.id
    wx.showModal({
      title: '删除',
      content: '您确定删除吗？',
      success: function (res) {
        if (res.confirm) {
          r.toPost('/jyxcAdvice/adviceDelete', {id:id}, (res) => {
            wx.showToast({
              title: '删除成功',
              icon:'none'
            })
            setTimeout(function(){
              that.setData({
                page: 1,
                pages: 0,
                list: []
              })
              that.datalist(1);
            },1000)
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },


  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (!this.loading && this.data.page < this.data.pages) {
      this.datalist(this.data.page + 1)
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})